{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from utils.test_data import data_gen\n",
    "from utils.hierarchical import hierarchical\n",
    "from utils.funcs import *\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "J=10#组数\n",
    "n=1000#每组样本数\n",
    "p=5#一层变量数(不含常数项)\n",
    "q=5#二层变量数(不含常数项)\n",
    "sigma2_y=1#y的方差\n",
    "sigma2_u=1#u的方差\n",
    "lamda=1#l1惩罚项的系数\n",
    "\n",
    "a=data_gen(J,p,q,n)\n",
    "mu=0#生成数据的均值\n",
    "sigma=1#生成数据的方差\n",
    "x=a.x_gen(0,1)\n",
    "w=a.w_gen(0,1)\n",
    "gamma=np.random.rand(p+1,q+1).reshape((-1,1),order=\"F\")+1\n",
    "gamma[int(gamma.shape[0]/2):,0]=0#将一半的系数置为0\n",
    "y=a.y_gen(gamma,sigma2_u,sigma2_y,w,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.624060</td>\n",
       "      <td>-1.493660</td>\n",
       "      <td>-2.090989</td>\n",
       "      <td>0.105251</td>\n",
       "      <td>-0.216023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.521166</td>\n",
       "      <td>0.416684</td>\n",
       "      <td>-0.621624</td>\n",
       "      <td>-1.490898</td>\n",
       "      <td>-0.031175</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.887820</td>\n",
       "      <td>-0.196162</td>\n",
       "      <td>0.252562</td>\n",
       "      <td>-2.033069</td>\n",
       "      <td>1.014290</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.663818</td>\n",
       "      <td>-0.321668</td>\n",
       "      <td>2.460085</td>\n",
       "      <td>-1.043005</td>\n",
       "      <td>-0.538174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.317514</td>\n",
       "      <td>-1.131302</td>\n",
       "      <td>-0.690612</td>\n",
       "      <td>-0.840028</td>\n",
       "      <td>-0.279777</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.841845</td>\n",
       "      <td>1.777337</td>\n",
       "      <td>0.244833</td>\n",
       "      <td>1.371724</td>\n",
       "      <td>-0.866654</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.240003</td>\n",
       "      <td>0.887031</td>\n",
       "      <td>0.379122</td>\n",
       "      <td>1.253187</td>\n",
       "      <td>-0.115449</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.329572</td>\n",
       "      <td>-0.925064</td>\n",
       "      <td>-1.461123</td>\n",
       "      <td>-0.068633</td>\n",
       "      <td>-0.683755</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.0</td>\n",
       "      <td>2.420252</td>\n",
       "      <td>-0.583253</td>\n",
       "      <td>-0.335134</td>\n",
       "      <td>0.276858</td>\n",
       "      <td>0.368642</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-1.872221</td>\n",
       "      <td>0.999707</td>\n",
       "      <td>-2.330536</td>\n",
       "      <td>1.070360</td>\n",
       "      <td>0.562749</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     0         1         2         3         4         5\n",
       "0  1.0  0.624060 -1.493660 -2.090989  0.105251 -0.216023\n",
       "1  1.0  0.521166  0.416684 -0.621624 -1.490898 -0.031175\n",
       "2  1.0  0.887820 -0.196162  0.252562 -2.033069  1.014290\n",
       "3  1.0 -1.663818 -0.321668  2.460085 -1.043005 -0.538174\n",
       "4  1.0 -1.317514 -1.131302 -0.690612 -0.840028 -0.279777\n",
       "5  1.0 -0.841845  1.777337  0.244833  1.371724 -0.866654\n",
       "6  1.0 -1.240003  0.887031  0.379122  1.253187 -0.115449\n",
       "7  1.0 -0.329572 -0.925064 -1.461123 -0.068633 -0.683755\n",
       "8  1.0  2.420252 -0.583253 -0.335134  0.276858  0.368642\n",
       "9  1.0 -1.872221  0.999707 -2.330536  1.070360  0.562749"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#展示数据\n",
    "pd.DataFrame(x[0]).head(10)#某一组x前10行的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "      <th>4</th>\n",
       "      <th>5</th>\n",
       "      <th>6</th>\n",
       "      <th>7</th>\n",
       "      <th>8</th>\n",
       "      <th>9</th>\n",
       "      <th>...</th>\n",
       "      <th>26</th>\n",
       "      <th>27</th>\n",
       "      <th>28</th>\n",
       "      <th>29</th>\n",
       "      <th>30</th>\n",
       "      <th>31</th>\n",
       "      <th>32</th>\n",
       "      <th>33</th>\n",
       "      <th>34</th>\n",
       "      <th>35</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.269338</td>\n",
       "      <td>0.054867</td>\n",
       "      <td>0.924079</td>\n",
       "      <td>-0.854752</td>\n",
       "      <td>-0.403065</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.269338</td>\n",
       "      <td>0.054867</td>\n",
       "      <td>0.924079</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.054867</td>\n",
       "      <td>0.924079</td>\n",
       "      <td>-0.854752</td>\n",
       "      <td>-0.403065</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.0</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>-0.000000</td>\n",
       "      <td>1.0</td>\n",
       "      <td>-0.269338</td>\n",
       "      <td>0.054867</td>\n",
       "      <td>0.924079</td>\n",
       "      <td>-0.854752</td>\n",
       "      <td>-0.403065</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6 rows × 36 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    0         1         2         3         4         5    6         7   \\\n",
       "0  1.0 -0.269338  0.054867  0.924079 -0.854752 -0.403065  0.0 -0.000000   \n",
       "1  0.0 -0.000000  0.000000  0.000000 -0.000000 -0.000000  1.0 -0.269338   \n",
       "2  0.0 -0.000000  0.000000  0.000000 -0.000000 -0.000000  0.0 -0.000000   \n",
       "3  0.0 -0.000000  0.000000  0.000000 -0.000000 -0.000000  0.0 -0.000000   \n",
       "4  0.0 -0.000000  0.000000  0.000000 -0.000000 -0.000000  0.0 -0.000000   \n",
       "5  0.0 -0.000000  0.000000  0.000000 -0.000000 -0.000000  0.0 -0.000000   \n",
       "\n",
       "         8         9   ...        26        27        28        29   30  \\\n",
       "0  0.000000  0.000000  ...  0.000000  0.000000 -0.000000 -0.000000  0.0   \n",
       "1  0.054867  0.924079  ...  0.000000  0.000000 -0.000000 -0.000000  0.0   \n",
       "2  0.000000  0.000000  ...  0.000000  0.000000 -0.000000 -0.000000  0.0   \n",
       "3  0.000000  0.000000  ...  0.000000  0.000000 -0.000000 -0.000000  0.0   \n",
       "4  0.000000  0.000000  ...  0.054867  0.924079 -0.854752 -0.403065  0.0   \n",
       "5  0.000000  0.000000  ...  0.000000  0.000000 -0.000000 -0.000000  1.0   \n",
       "\n",
       "         31        32        33        34        35  \n",
       "0 -0.000000  0.000000  0.000000 -0.000000 -0.000000  \n",
       "1 -0.000000  0.000000  0.000000 -0.000000 -0.000000  \n",
       "2 -0.000000  0.000000  0.000000 -0.000000 -0.000000  \n",
       "3 -0.000000  0.000000  0.000000 -0.000000 -0.000000  \n",
       "4 -0.000000  0.000000  0.000000 -0.000000 -0.000000  \n",
       "5 -0.269338  0.054867  0.924079 -0.854752 -0.403065  \n",
       "\n",
       "[6 rows x 36 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#展示数据\n",
    "pd.DataFrame(w[0]).head(10)#某一组w前10行的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.470662</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2.465567</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.565347</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.366368</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.029273</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>3.668505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2.065666</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.773867</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>-1.906122</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.174413</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0\n",
       "0 -1.470662\n",
       "1  2.465567\n",
       "2 -0.565347\n",
       "3  2.366368\n",
       "4  2.029273\n",
       "5  3.668505\n",
       "6  2.065666\n",
       "7  0.773867\n",
       "8 -1.906122\n",
       "9  1.174413"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#展示数据\n",
    "pd.DataFrame(y[0]).head(10)#某一组y前10行的数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.349186</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.336491</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.260274</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.047066</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.453738</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.387253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>1.505504</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>1.707696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.791297</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>1.124725</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1.746174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>1.079839</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.490874</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1.604909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.830912</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>1.110394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1.189353</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>1.037699</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           0\n",
       "0   1.349186\n",
       "1   1.336491\n",
       "2   1.260274\n",
       "3   1.047066\n",
       "4   1.453738\n",
       "5   1.387253\n",
       "6   1.505504\n",
       "7   1.707696\n",
       "8   1.791297\n",
       "9   1.124725\n",
       "10  1.746174\n",
       "11  1.079839\n",
       "12  1.490874\n",
       "13  1.604909\n",
       "14  1.830912\n",
       "15  1.110394\n",
       "16  1.189353\n",
       "17  1.037699\n",
       "18  0.000000\n",
       "19  0.000000\n",
       "20  0.000000\n",
       "21  0.000000\n",
       "22  0.000000\n",
       "23  0.000000\n",
       "24  0.000000\n",
       "25  0.000000\n",
       "26  0.000000\n",
       "27  0.000000\n",
       "28  0.000000\n",
       "29  0.000000\n",
       "30  0.000000\n",
       "31  0.000000\n",
       "32  0.000000\n",
       "33  0.000000\n",
       "34  0.000000\n",
       "35  0.000000"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#展示数据\n",
    "pd.DataFrame(gamma)#gamma数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "b=hierarchical(y,x,w)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "已经迭代100次\n",
      "已经迭代200次\n",
      "已经迭代300次\n",
      "已经迭代400次\n",
      "已经迭代500次\n",
      "已经迭代600次\n",
      "已经迭代700次\n",
      "已经迭代800次\n",
      "已经迭代900次\n",
      "已经迭代1000次\n",
      "已经迭代1100次\n",
      "已经迭代1200次\n",
      "已经迭代1300次\n",
      "已经迭代1400次\n",
      "已经迭代1500次\n",
      "已经迭代1600次\n",
      "已经迭代1700次\n",
      "已经迭代1800次\n",
      "已经迭代1900次\n",
      "已经迭代2000次\n",
      "已经迭代2100次\n",
      "已经迭代2200次\n",
      "已经迭代2300次\n",
      "已经迭代2400次\n",
      "已经迭代2500次\n",
      "已经迭代2600次\n",
      "已经迭代2700次\n",
      "已经迭代2800次\n",
      "已经迭代2900次\n",
      "已经迭代3000次\n",
      "已经迭代3100次\n",
      "已经迭代3200次\n",
      "已经迭代3300次\n",
      "已经迭代3400次\n",
      "已经迭代3500次\n",
      "已经迭代3600次\n",
      "已经迭代3700次\n",
      "已经迭代3800次\n",
      "已经迭代3900次\n",
      "已经迭代4000次\n",
      "已经迭代4100次\n",
      "已经迭代4200次\n",
      "已经迭代4300次\n",
      "已经迭代4400次\n",
      "已经迭代4500次\n",
      "已经迭代4600次\n",
      "已经迭代4700次\n",
      "已经迭代4800次\n",
      "已经迭代4900次\n",
      "已经迭代5000次\n",
      "已经迭代5100次\n",
      "已经迭代5200次\n",
      "已经迭代5300次\n",
      "已经迭代5400次\n",
      "已经迭代5500次\n",
      "已经迭代5600次\n",
      "已经迭代5700次\n",
      "已经迭代5800次\n",
      "已经迭代5900次\n",
      "已经迭代6000次\n",
      "已经迭代6100次\n",
      "已经迭代6200次\n",
      "已经迭代6300次\n",
      "已经迭代6400次\n",
      "已经迭代6500次\n",
      "已经迭代6600次\n",
      "已经迭代6700次\n",
      "已经迭代6800次\n",
      "已经迭代6900次\n",
      "已经迭代7000次\n",
      "已经迭代7100次\n",
      "已经迭代7200次\n",
      "已经迭代7300次\n",
      "已经迭代7400次\n",
      "已经迭代7500次\n",
      "已经迭代7600次\n",
      "已经迭代7700次\n",
      "已经迭代7800次\n",
      "已经迭代7900次\n",
      "已经迭代8000次\n",
      "已经迭代8100次\n",
      "已经迭代8200次\n",
      "已经迭代8300次\n",
      "已经迭代8400次\n",
      "已经迭代8500次\n",
      "已经迭代8600次\n",
      "已经迭代8700次\n",
      "已经迭代8800次\n",
      "已经迭代8900次\n",
      "已经迭代9000次\n",
      "已经迭代9100次\n",
      "已经迭代9200次\n",
      "已经迭代9300次\n",
      "已经迭代9400次\n",
      "已经迭代9500次\n",
      "已经迭代9600次\n",
      "已经迭代9700次\n",
      "已经迭代9800次\n",
      "已经迭代9900次\n",
      "已经迭代10000次\n",
      "模型未收敛\n"
     ]
    }
   ],
   "source": [
    "tol=1e-7\n",
    "max_iter=1e4\n",
    "lamda=1\n",
    "b.update(tol,max_iter,lamda)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0.831371</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.689036</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.701167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.540799</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0.834808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.370794</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.311805</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.642733</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>1.636436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>0.156884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>1.058958</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>-0.007403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1.043438</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>1.067431</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>1.060456</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>0.298656</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>0.009043</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>0.829577</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>0.145865</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>-0.000008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>-0.317165</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>-0.000873</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>-0.000188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>0.138019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>-0.000055</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>-0.211447</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>-0.345619</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>0.000222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>-0.144657</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>1.006392</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>0.000002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>-0.014646</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>-0.141400</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>0.377042</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           0\n",
       "0   0.831371\n",
       "1   0.689036\n",
       "2   0.701167\n",
       "3   0.540799\n",
       "4   0.834808\n",
       "5   0.370794\n",
       "6   0.311805\n",
       "7   0.642733\n",
       "8   1.636436\n",
       "9   0.156884\n",
       "10  1.058958\n",
       "11 -0.007403\n",
       "12  1.043438\n",
       "13  1.067431\n",
       "14  1.060456\n",
       "15  0.298656\n",
       "16  0.009043\n",
       "17  0.829577\n",
       "18  0.145865\n",
       "19  0.000000\n",
       "20 -0.000008\n",
       "21 -0.317165\n",
       "22 -0.000873\n",
       "23 -0.000188\n",
       "24  0.138019\n",
       "25 -0.000055\n",
       "26 -0.211447\n",
       "27 -0.345619\n",
       "28  0.000222\n",
       "29  0.000000\n",
       "30 -0.144657\n",
       "31  1.006392\n",
       "32  0.000002\n",
       "33 -0.014646\n",
       "34 -0.141400\n",
       "35  0.377042"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#拟合结果展示\n",
    "pd.DataFrame(b.gamma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9929520284238273"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sigma2_y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.156284113000982"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b.sigma2_u"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}